import axios from 'axios';
import sync_request from 'sync-request';
import {Message} from 'element-ui';

// axios 配置
axios.defaults.timeout = 50000;
axios.defaults.withCredentials = true;
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

//分离部署，配置URL
if (process.env.NODE_ENV == 'test') {
    axios.defaults.baseURL = "http://test-reg.seedeer.com:88/bbc";
} else if (process.env.NODE_ENV == 'prod') {
    axios.defaults.baseURL = "https://mk.seedeer.com/bbc";
}

// http request 拦截器
axios.interceptors.request.use(
    config => {
        return config;
    },
    err => {
        Message({
            message: err.message,
            type: 'error',
            duration: 5 * 1000
        });
        return Promise.reject(err);
    }
);

// http response 拦截器
axios.interceptors.response.use(
    response => {
        // console.log(response)
        //缓存多语言版本号
        if (response.headers.version){
            localStorage.setItem("bbc_version", response.headers.version)
        }
        return response;
    },
    error => {
        // console.log(error)
        if(error && error.response) {
            switch (error.response.status) {
                // FIXME message 多语言配置
                case 403: {
                    Message({
                        message: "无权限，请联系管理员！",
                        type: 'error',
                        duration: 5 * 1000
                    });
                    break;
                }
                case 404: {
                    Message({
                        message: "地址不存在，请检查是否操作正确或联系管理员！",
                        type: 'error',
                        duration: 5 * 1000
                    });
                    break;
                }
                default : {
                    Message({
                        message: "系统错误，请稍后再试或联系管理员！",
                        type: 'error',
                        duration: 5 * 1000
                    });
                }
            }
        }

        return Promise.reject(error);   // 返回接口返回的错误信息
    }
);

export function axios_get(url, params) {
    return new Promise((resolve, reject) => {
        axios.get(url, params)
            .then(response => {
                //console.log("orinal response.data = " + JSON.stringify(response.data));
                //console.log("orinal response.data = " + response.data);
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            })
    })
}

export function axios_post(url, params) {
    //console.log("params = " + JSON.stringify(params));
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

export function axios_put(url, params) {
    //console.log("params = " + JSON.stringify(params));
    return new Promise((resolve, reject) => {
        axios.put(url, params)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

export function axios_delete(url) {
    return new Promise((resolve, reject) => {
        axios.delete(url)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error);
            });
    });
}

export default {
    axios: function() {
        return axios;
    },

    get: function(url, params) {
        return axios_get(url, params);
    },

    post: function(url, params) {
        return axios_post(url, params);
    },

    put: function(url, params) {
        return axios_put(url, params);
    },

    delete: function(url) {
        return axios_delete(url);
    },

    sync_get: function (url) {
        let response = sync_request('GET', url);
        return response['body'];
    }
}
